<template>
  <van-nav-bar
    title="注册"
    left-text="返回"
    left-arrow
    @click-left="$router.back()"
    placeholder
    fixed
  />

  <van-form @submit="onSubmit" @failed="onFailed">
    <van-cell-group inset>
      <van-field
        v-model="username"
        name="用户名"
        label="用户名"
        placeholder="用户名"
        :rules="[{ pattern: /\w{3,}/, message: '三位以上 的数字字母下划线' }]"
      />
      <van-field
        v-model="password"
        type="password"
        name="密码"
        label="密码"
        placeholder="密码"
        :rules="[{ pattern: /.{6,}/, message: '三位以上 的任意字符' }]"
      />
    </van-cell-group>
    <div style="margin: 16px">
      <van-button round block type="primary" native-type="submit"> 注册 </van-button>
    </div>
  </van-form>
</template>

<script setup>
import { ref } from 'vue'
import { register } from '@/api/user'
import { useRouter } from 'vue-router'
let router = useRouter()
let username = ref('')
let password = ref('')
async function onSubmit() {
  let r = await register(username.value, password.value)
  console.log(r.data)
  if (r.data.code == 1) {
    showSuccessToast('注册成功')
    router.push('/login')
  } else {
    showFailToast('注册失败')
  }
}
</script>

<style>
.reg {
  width: 90%;
  margin: 0 auto;
}
</style>
